package com.sinobpo.rnd2.ehr.web.action;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionSupport;
import com.sinobpo.rnd2.ehr.dao.HosNumSatDao;
import com.sinobpo.rnd2.ehr.model.ClinicSat;
import com.sinobpo.rnd2.ehr.model.HosNumSat;
import com.sinobpo.rnd2.ehr.util.CommonUtil;
import com.sinobpo.rnd2.ehr.util.JsonDateValueProcessor;
import com.sinobpo.rnd2.ehr.util.PieDataType;
import com.sinobpo.rnd2.ehr.util.TimeType;

/**
 * 住院统计管理类
 * 
 * @author yinghui.zhang
 * @CreateDate 2012-7-20
 * @file HosNumManageAction.java
 * @package com.sinobpo.rnd2.ehr.web.action
 * @version 1.0
 * @Email uuhui@163.com
 */
@Controller("hosNumManageAction")
@Scope("prototype")
public class HosNumManageAction extends ActionSupport {
	/**
	 * 序列号
	 */
	private static final long serialVersionUID = 1L;
	@Resource(name = "hosNumSatDaoImpl")
	private HosNumSatDao hosNumSatDao;
	private JSONObject jsonObj;
	private JSONArray jsonArray;
	private String orgCode; // 机构代码
	private String startDate;// 开始时间
	private String endDate;// 截止时间
	private int page; // jquery easyUi dataGrid 用于分页数据的当前页数
	private int rows; // jquery easyUI dataGrid 用于分页数据的每页数据

	/**
	 * 综合统计饼图表
	 * 
	 * @return
	 */
	public String getSumPie() {
		Object[] obj = (Object[]) hosNumSatDao.getSumByType(getTimeType(),
				orgCode, startDate, endDate);
		if (obj[0] == null && obj[1] == null && obj[2] == null)
			jsonArray = null;
		else
			jsonArray = JSONArray.fromObject(CommonUtil.getPieChartData(obj,
					PieDataType.HosNumPie));
		return "jsonArray";
	}

	/**
	 * 设置日期格式
	 * 
	 * @return
	 */
	private TimeType getTimeType() {
		if (startDate.length() > 4)
			return TimeType.DAY;
		else if (startDate.length() == 2)
			return TimeType.MONTH;
		else
			return TimeType.YEAR;
	}

	/**
	 * 综合统计column及line图表
	 * 
	 * @return
	 */
	public String getSumChart() {
		List<HosNumSat> list = hosNumSatDao.getSum(getTimeType(), orgCode,
				startDate, endDate);
		if (list.size() == 0)
			jsonObj = null;
		else
			jsonObj = JSONObject.fromObject(CommonUtil.getChartOfHosNumSatData(
					getTimeType(), list));
		return SUCCESS;
	}

	/**
	 * 综合统计查询数据明细信息
	 * 
	 * @return
	 */
	public String getSumList() {
		JsonConfig jsonConfig = new JsonConfig();
		jsonConfig.registerJsonValueProcessor(Date.class,
				new JsonDateValueProcessor());
		List<HosNumSat> list = hosNumSatDao.getList(page, rows, getTimeType(),
				orgCode, startDate, endDate);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("total", hosNumSatDao.getCount(getTimeType(), orgCode,
				startDate, endDate));
		map.put("rows", list);
		jsonObj = JSONObject.fromObject(map, jsonConfig);
		jsonConfig = null;
		return SUCCESS;
	}

	public JSONObject getJsonObj() {
		return jsonObj;
	}

	public void setJsonObj(JSONObject jsonObj) {
		this.jsonObj = jsonObj;
	}

	public JSONArray getJsonArray() {
		return jsonArray;
	}

	public void setJsonArray(JSONArray jsonArray) {
		this.jsonArray = jsonArray;
	}

	public String getOrgCode() {
		return orgCode;
	}

	public void setOrgCode(String orgCode) {
		this.orgCode = orgCode;
	}

	public String getStartDate() {
		return startDate;
	}

	public void setStartDate(String startDate) {
		this.startDate = startDate;
	}

	public String getEndDate() {
		return endDate;
	}

	public void setEndDate(String endDate) {
		this.endDate = endDate;
	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public int getRows() {
		return rows;
	}

	public void setRows(int rows) {
		this.rows = rows;
	}

}
